﻿
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel;
using System;
using System.Reflection;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Web.Services.Protocols;
using ApplicationGenerator.BusinessModule.Elements;
using ApplicationGenerator.BusinessModule.Interfaces;
using System.ComponentModel.Design;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.CommonForms;
using System.Collections.Generic;

namespace WorkflowLibrary
{
    /// <summary>
    /// Aktywność do wywoływania websevice
    /// </summary>

    public partial class InvokeWebServiceActivity : Activity, IWebServiceInvocation, IActivityName//, ICustomTypeDescriptor
    {
        #region Construction

        public InvokeWebServiceActivity()
        {
            InitializeComponent();
            ActivityName = "Wywolaj web serwis";
        }

        #endregion

        #region Activity execution

        protected override ActivityExecutionStatus Execute
            (ActivityExecutionContext context)
        {
            WebServiceInvoker webServiceInvoker = new WebServiceInvoker();
            try
            {
                webServiceInvoker.Invoke(this, (Parent as GeneratorSequentialWorkflow).InstanceId);
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd w trakcie wywoływania metody web serwisowej", ex);
            }

            return ActivityExecutionStatus.Closed;
        }

        #endregion

        #region Dependecy properties
        public static DependencyProperty MappingsConfigurationProperty =
                    DependencyProperty.Register("MappingsConfiguration",
                    typeof(byte[]),
                    typeof(InvokeWebServiceActivity));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("MappingsConfiguration")]
        [CategoryAttribute("InvokeWebService Property")]
        [Editor(typeof(MappingsEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(MappingsConverter))]
        public byte[] MappingsConfiguration
        {
            get
            {
                return (byte[])(base.GetValue(InvokeWebServiceActivity.MappingsConfigurationProperty));
            }
            set
            {
                base.SetValue(InvokeWebServiceActivity.MappingsConfigurationProperty, value);
            }
        }

        public static DependencyProperty WebServiceURLProperty =
                    DependencyProperty.Register("WebServiceURL",
                    typeof(System.String),
                    typeof(InvokeWebServiceActivity));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        [DescriptionAttribute("Adres URL web serwisu")]
        [CategoryAttribute("InvokeWebServiceActivity Property")]
        public string WebServiceURL
        {
            get
            {
                return ((string)(base.GetValue(InvokeWebServiceActivity.WebServiceURLProperty)));
            }
            set
            {
                base.SetValue(InvokeWebServiceActivity.WebServiceURLProperty, value);
            }
        }

        public static DependencyProperty MethodNameProperty =
                    DependencyProperty.Register("MethodName",
                    typeof(System.String),
                    typeof(InvokeWebServiceActivity));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("Nazwa metody do wykonania")]
        [Editor(typeof(MethodNameEditorWrapper), typeof(UITypeEditor))]
        [CategoryAttribute("InvokeWebServiceActivity Property")]
        public string MethodName
        {
            get
            {
                return ((string)(base.GetValue(InvokeWebServiceActivity.MethodNameProperty)));
            }
            set
            {
                base.SetValue(InvokeWebServiceActivity.MethodNameProperty, value);
            }
        }

        /// <summary>
        /// Nazwa meta modelu, na podstawie którego będą wybierane mapowania
        /// </summary>
        [Browsable(false)]
        public string MetaModelName
        {
            get
            {
                if ((Parent as GeneratorSequentialWorkflow).MetaModel == null)
                {
                    return null;
                }

                return (Parent as GeneratorSequentialWorkflow).MetaModel.Name;
            }
        }
        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["ActivityName"]);
            colDisplayProperties.Add(colAvailableCollection["WebServiceURL"]);
            colDisplayProperties.Add(colAvailableCollection["MethodName"]);
            colDisplayProperties.Add(colAvailableCollection["MappingsConfiguration"]);

            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        #region IActivityName Members
        [Category("Dane podstawowe")]
        [DisplayName("Nazwa czynności")]
        [Description("Nazwa czynności przyjazna dla użytkownika")]
        public string ActivityName
        {
            get;
            set;
        }

        #endregion

    }
}
